local FZutil=require("app.FZutil")
local WaitingLayer=class("WaitingLayer")

WaitingLayer.TYPE_CONNECT_ING=1             --等待
WaitingLayer.TYPE_RELOGIN=2                 --重新登录
WaitingLayer.TYPE_RECONNECT_RELOGIN=3       --重连或者重新登录

local WaitingLayerUI=class("WaitingLayerUI",FZutil.createUILayer)

function WaitingLayerUI:ctor(type,loader,callback)
    self.type=type
    self.loader=loader
    self.callback=callback
    self:setBackGroundColorType(ccui.LayoutBackGroundColorType.none)
    self:enableNodeEvents()
    local uimap=FZutil.createSceneNode("Connect")
    self:addChild(uimap.root)
    uimap.root:setPosition(display.center)
    self.uimap=uimap
    uimap.Button_reconnect:onTouch(function(e)      --返回登录（控件名字命名反了）
        if e.name=="ended" then           
            local tempcall=self.callback
            self.loader:hide()
            if tempcall then 
                tempcall()
            end
        end
    end)
    uimap.Button_relogin:onTouch(function(e)        --重连
        if e.name=="ended" then
            MyApp:restart()
        end
    end)
    uimap.Button_relogin_01:onTouch(function( e )  --返回登录
        if e.name=="ended" then 
            MyApp:restart()
        end
    end)

	self:setType(type,callback)    
end

function WaitingLayerUI:setType(type,callback)
	local uimap=self.uimap
    self.callback=callback
	if type==WaitingLayer.TYPE_CONNECT_ING then 
        uimap.Button_reconnect:hide()
        uimap.Button_relogin:hide()
        uimap.Button_relogin_01:hide()
        uimap.UITX_lianjie:getAnimation():play("action01")
    elseif type==WaitingLayer.TYPE_RELOGIN then 
        uimap.UITX_lianjie:getAnimation():play("action02")
        uimap.Button_reconnect:hide()
        uimap.Button_relogin:hide()
        uimap.Button_relogin_01:show()
    else
        uimap.UITX_lianjie:getAnimation():play("action02")
        uimap.Button_reconnect:show()
        uimap.Button_relogin:show()
        uimap.Button_relogin_01:hide()
    end
end

--------------------------------------------------------------------------------------------

function WaitingLayer:ctor()
    self.layer=nil
end

function WaitingLayer:show(connect_type,callback)
    if DEBUG>=3 then 
        assert(self.layer==nil,"layer has show")
        FZutil.printCallStack()
        print("===============printCallStack show =======================")
    end
    if self.layer then 
        self.layer:setType(connect_type,callback)
    else
        self.layer=WaitingLayerUI.new(connect_type,self,callback)
    end
    display.getRunningScene():addChild(self.layer,1000)
end

function WaitingLayer:hide()
    if DEBUG>=3 then 
        FZutil.printCallStack()
        print("===============printCallStack hide WaitingLayer =======================")
    end
    if self.layer then 
        self.layer:removeSelf()
        self.layer=nil
    end
end

return WaitingLayer